package service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import common.PageReturn;
import dao.EssayDao;
import dao.UserMapper;
import jdk.nashorn.internal.runtime.arrays.IteratorAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.UserDao;
import pojo.*;

@Service
public class UserServiceImpl implements UserServiceI
{

	@Autowired
	private UserDao userdao;
	@Autowired
	private UserMapper uMapper;
	@Autowired
	private  MessageServiceI messageServiceI;
	@Autowired
	private EssayDao essayDao;
	
	@Override
	public User Login(String username) {
		// TODO Auto-generated method stub
		return userdao.selectByUserName(username);
	}
	
	@Override
	public int Register(User user) {
		// TODO Auto-generated method stub
		return uMapper.insertSelective(user);
	}


	@Override
	public User FindUserById(Integer userId) {
		// TODO Auto-generated method stub
		return uMapper.selectByPrimaryKey(userId);
	}

	@Override
	public int CompleteUserInformation(User user) {
		// TODO Auto-generated method stub
		return uMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public int ResetPassword(User user) {
		// TODO Auto-generated method stub
		return uMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public List<User> FindAllUser(Page page) {
		// TODO Auto-generated method stub
		PageReturn pageReturn=new PageReturn();
		page=pageReturn.ReturnPage(page);
		return userdao.selectAllUser(page);
	}

	@Override
	public List<User> FindUserByText(String text,Page page) {
		PageReturn pageReturn=new PageReturn();
		page=pageReturn.ReturnPage(page);
		return userdao.selectByUserNameText(text,page);
	}

	@Override
	public List<User> FindUserByUserName(String username,Page page) {
		return userdao.selectByUserNamePage(username,page);
	}

	@Override
	public int FindUserByUserNameCount(String username) {
		return userdao.selectByUserNamePageCount(username);
	}

	@Override
	public List<User> FindByUserId(Integer userId) {
		return userdao.SelectByUserId(userId);
	}

	@Override
	public boolean DeleteById(Integer userId) {
		int i= uMapper.deleteByPrimaryKey(userId);
		if(i>0){
			return  true;
		}else
			return  false;
	}

	@Override
	public List<Integer> SelectAttention(Integer userId) {
		List<Integer> list = userdao.SelectAttentionUserId(userId);
		List<Integer> list2=userdao.SelectAttentionUser2Id(userId);
		List<Integer> list1=new ArrayList<Integer>();
		for (Integer user_Id : list) {
			list1.add(user_Id);
		}
		for (Integer integer : list2) {
			list1.add(integer);
		}
		return list1;
	}

	@Override
	public boolean MakeAttentionMessage(Integer userId, String username,String essayTitle,String date) {

		String message="你关注的 "+username+" 新发布了文章《"+essayTitle+"》,快去看看吧";
		Message message1=new Message();
		message1.setReadornot(0);
		message1.setSuserId(-1);
		message1.setRuserId(userId);
		message1.setMessageContent(message);
		message1.setMessageDate(date);
		try {
			messageServiceI.MakeAttentionMessage(message1);
		}catch (Exception e){
			e.printStackTrace();
			return  false;
		}
		return true;
	}

	@Override
	public List<EssayAndUser> FindOtherUserEssayLogin(Integer userId,Page page) {
		String permission="B_self";
		List<EssayAndUser> list=essayDao.SelectOtherUserEssayLogin(userId,permission,page);
		return list;
	}

    @Override
    public Integer FindOtherUserEssayCountLogin(Integer userId) {
        String permission="B_self";
        return essayDao.SelectOtherUserEssayCountLogin(userId,permission);
    }

    @Override
    public List<EssayAndUser> FindOtherUserEssayLogout(Integer userId, Page page) {
        String permission="B_all";
        List<EssayAndUser> list=essayDao.SelectOtherUserEssaylogout(userId,permission,page);
        return list;
    }

    @Override
    public Integer FindOtherUserEssayCountLogout(Integer userId) {
        String permission="B_all";
        return essayDao.SelectOtherUserEssayCountlogout(userId,permission);
    }

}
